package fixtures;

import java.io.IOException;

import client.CCRMClient;
import client.CcrmApp;
import model.CCRMClientModel;
import model.Employee;
import model.Envelope;
import model.LoginMod;
import model.User;
import view.CCRMClientGUI;
import view.CostumerRelEmpGui;
import view.LoginGui1;
import view.MarketingManagerGui;
import view.StartCampignGui;
import controllers.CCRMClientController;
import controllers.MarketingManagController;
import controllers.*;
import controllers.LoginController;
import controllers.StartCampignController;
import fit.ActionFixture;

public class ActivateCampaignAcceptanceTest extends ActionFixture {

/**
 * The Class InsertingCustomersDatailsAccepptenceTest.
 * @author Gili Barak.
 * @author Moran Abecassis.
 */
		
		/** The customer relations employee view. */
		private StartCampignGui view;
		
		/** The user model for customer. */
		private User mod;
		
		/** The customer relations employee controller. */
		private StartCampignController con;
		
		/** The customer relations employee details. */
		private Employee emp;
		
		/** The login controller. */
		private LoginController prevCon;
		
		private MarketingManagController MarketingManagController;
		
		/** The log view. */
		private LoginGui1 logView;
		
		/** The log model. */
		private LoginMod logModel;
		
		/** The log controller. */
		private LoginController logController;
		
		private CCRMClientGUI clientView;
		
		private CCRMClientModel clientModel;
		
		private CCRMClientController clientController;
	
		
		//public static CCRMClient clien;
		
		
		/**
		 * Instantiates a new Inserting Customers details acceptance test.
		 */
		public ActivateCampaignAcceptanceTest ()
		{
			clientView = new CCRMClientGUI();
			clientView.setVisible(false);
			clientModel = new CCRMClientModel();
			clientController = new CCRMClientController(clientView,clientModel);
			
			clientView.getTextHost().setText("localhost");
			clientView.getTextPort().setText("5555");
			//clientView.getButtonOk().doClick();
			
			
			//replace button click OK***********************
			try 
			{
				String host = new String (clientView.getHost());
				int port = clientView.getPort();
				clientModel.setHost(host);
				clientModel.setPort(port);
				
				CcrmApp.clien = new CCRMClient(host,port); //singleton
				
				if(CcrmApp.clien.isConnected())
				{
					clientView.dispose(); //remove the current window to display login window
				
					//create a new login controller
					 logView = new LoginGui1();
					 logModel = new LoginMod();
					 logController = new LoginController(logView,logModel);
					 logView.setVisible(false);
				}
				else
				{
					clientView.displayWarnningMessage("Faild to connect. check IP and port!");
					clientView.clearFields();
				}
				
			} catch (NumberFormatException e) 
			{
				clientView.displayWarnningMessage("Faild to connect. check IP and port!");
				clientView.clearFields();
				//e.printStackTrace();
			} 
			catch (IOException e) 
			{
				clientView.displayWarnningMessage("Connection problem. check IP and Port.");
				//e.printStackTrace();
			}
		
			
			
			//*********************************************
			/*
			if(!connectToServer(5555, "localhost"))
				{
					System.out.println("faild to connect!");
					return;
				}
			*/
				//logout user manually - if connected
			
				/*String sql = "UPDATE employee SET status= '0' WHERE employee.UserName = 'jac' AND employee.password='123';";
				try {
					CcrmApp.clien.sendToServer(sql);
					Thread.sleep(500);
				} catch (Exception e) {
					e.printStackTrace();
				}
				*/
				//create a new login controller
				//logView = new LoginGui1();
				
				logView.setVisible(false);   // Set the visibility to false
				
				//CcrmApp.clien.addObserver(logController);
				
				//do the logging in
				logView.getTextFieldUser().setText("moa");
				logView.getTextFieldPass().setText("moa");
				emp = new Employee("123",1,"moa","moa",2,0);
				//logView.getLogin().doClick();
				
				
				//replace click login*************************************************

	        	  
      		  String pass = logView.getTextPassword();
      		  String user = logView.getTextUserName();
      		  
      		  if(pass.equals("")|| user.equals("") ) {
      			logView.setWarningMessageVisibleTrue();
      			  return;
      		  }
      		  else
      		  {
      			logView.undisplayWarningMessage();
      			  try{ //set the user name and password and sent to server
      				logModel.setPassword(pass);
      				logModel.setUserName(user);
      				 Envelope en = new Envelope(logModel,"searchLogin");
  
      				 CcrmApp.clien.sendToServer(en);
      				 CcrmApp.clien.setCurrObj(logController);
      				 
      			  }
      			  catch(Exception e){
      				  
      			  }
			
		      }
				
				//***********************************************************************
				
      		  
      		  
				
			/*
				try {
					
					//logModel.setPassword(pass);
				//	logModel.setUserName(user);
   				 Envelope en = new Envelope(logModel,"searchLogin");
   				logController.sendToServer(en);
					//CcrmApp.clien.sendToServer(en);
					Thread.sleep(500);
				} catch (Exception e) {
					e.printStackTrace();
				}
				logController.getLoginG().setVisible(false);
				//CcrmApp.clien.deleteObservers();
				
				*/
      		  
				MarketingManagerGui MarkManGui = new MarketingManagerGui();
				MarketingManagController MarkManCon = new MarketingManagController(emp,MarkManGui,logController);
				MarkManGui.setVisible(false);
				
				
				
				//create a new customer relations controller
				view = new StartCampignGui();
				view.setVisible(false);   // Set the visibility to false
				//mod = new User();
				con = new StartCampignController(MarkManCon,view);
				//CcrmApp.clien.addObserver(cusController);
			}
			
			/**
			 * Enter id.
			 *
			 * @param str the str
			 */
			public void chooseCampaign(String str){
				view.getCampigncomboBox().setSelectedIndex(Integer.parseInt(str));;
			}
		
			
			
			/**
			 * Check warning message.
			 *
			 * @return the string
			 */
			public String checkWarningMessage(){
				view.getBtnStartButton().doClick();
				try{
				Thread.sleep(1000);
				}
				catch(Exception e){
					
				}
				//view.setWarningMessageVisibleTrue("");
				return view.getLblwarning().getText();
			}
			
			/**
			 * This method validates if both the "Host" name and the "Port" were entered.
			 *
			 * @param host the host
			 * @param port the port
			 * @return false if the connection crashed, else returns true
			 */
			
			private boolean validateInput(String host, int port) 
			{
				return (!host.equals("") && port!=0);
			}
			
			/**
			 * Connect to server.
			 *
			 * @param port the port
			 * @param host the host
			 * @return true, if successful
			 */
			
			private boolean connectToServer(int port, String host)
			{
				if(!validateInput(host, port))
					return false;
				try 
				{
					CcrmApp.clien = new CCRMClient(host,port);
					
					if(!CcrmApp.clien.isConnected())
					{
						System.out.println("Faild to connect. check IP and port!");
						return false;
					}
					
					return true;
				
				} catch (NumberFormatException e) 
				{
					System.out.println("Faild to connect. check IP and port!");
					e.printStackTrace();
					return false;
				} 
				catch (IOException e) 
				{
					System.out.println("Connection problem. check IP and Port.");
					e.printStackTrace();
					return false;
				}
			}
			
}
